//
//  ZNWBMeiTuanRefreshView.swift
//  ZNWeibo
//
//  Created by FunctionMaker on 2017/8/29.
//  Copyright © 2017年 FunctionMaker. All rights reserved.
//

import UIKit

class ZNWBMeiTuanRefreshView: ZNWBRefreshView {

    @IBOutlet weak var buildingIconView: UIImageView!
    @IBOutlet weak var earthIconView: UIImageView!
    @IBOutlet weak var kangarooIconView: UIImageView!
    
    override var parentViewHeight: CGFloat {
        didSet {
            if parentViewHeight < ZNWBRefreshIconScalerOffset || parentViewHeight > ZNWBRefreshOffset {
                return
            }
            
            // 改变袋鼠的大小
            let scaler = (parentViewHeight - ZNWBRefreshIconScalerOffset) / (ZNWBRefreshOffset - ZNWBRefreshIconScalerOffset) * (1 - ZNWBRefreshIconOriginalScale) + ZNWBRefreshIconOriginalScale
            kangarooIconView.transform = CGAffineTransform(scaleX: scaler, y: scaler)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // 设置房子的动画
        let bImage1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let bImage2 = #imageLiteral(resourceName: "icon_building_loading_2")
        
        buildingIconView.image = UIImage.animatedImage(with: [bImage1, bImage2], duration: 0.5)
        
        // 设置地球的动画
        let rotationAni = CABasicAnimation(keyPath: "transform.rotation")
        rotationAni.toValue = -2.0 * .pi
        rotationAni.repeatCount = MAXFLOAT
        rotationAni.duration = 3.0
        rotationAni.isRemovedOnCompletion = false
        
        earthIconView.layer.add(rotationAni, forKey: "rotation")
        
        // 设置袋鼠的动画
        let kanImage1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1");
        let kanImage2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2");
        
        kangarooIconView.image = UIImage.animatedImage(with: [kanImage1, kanImage2], duration: 0.5);
        
        // 改变了锚点，视图的位置需要更新
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        // 重新调整视图位置
        kangarooIconView.center = CGPoint(x: self.bounds.width / 2.0, y: self.bounds.height - ZNWBRefreshIconScalerOffset)
        
        kangarooIconView.transform = CGAffineTransform(scaleX: ZNWBRefreshIconOriginalScale, y: ZNWBRefreshIconOriginalScale)
    }
}
